package com.example.shopping2

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.example.perplonews.a_d_customview.Bottom_VIew
import com.example.shopping2.a_c_fragment.car.CarFragment
import com.example.shopping2.a_c_fragment.classa.ClassFragment
import com.example.shopping2.a_c_fragment.home.HomeFragment
import com.example.shopping2.a_c_fragment.main.LoginActivity
import com.example.shopping2.a_c_fragment.main.MainFragment
import com.example.shopping2.a_c_fragment.msg.MsgFragment
import com.example.shopping2.a_c_network.LjlUtils

class MainActivity : AppCompatActivity(), Bottom_VIew.ClickBar {
    private val main_home: Bottom_VIew by lazy { findViewById<Bottom_VIew>(R.id.main_home) }
    private val main_class: Bottom_VIew by lazy { findViewById<Bottom_VIew>(R.id.main_class) }
    private val main_car: Bottom_VIew by lazy { findViewById<Bottom_VIew>(R.id.main_car) }
    private val main_msg: Bottom_VIew by lazy { findViewById<Bottom_VIew>(R.id.main_msg) }
    private val main_main: Bottom_VIew by lazy { findViewById<Bottom_VIew>(R.id.main_main) }

    private var homeFragment = HomeFragment()
    private var classFragment = ClassFragment()
    private var carFragment = CarFragment()
    private var msgFragment = MsgFragment()
    private var mainFragment = MainFragment()
    private var boolean = false
    private var chouse = 0
    var mybro = MyBro()
    private var type: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //注册广播
        var intentfilter = IntentFilter()
        intentfilter.addAction("user_login")
        intentfilter.addAction("login_finish")
        registerReceiver(mybro, intentfilter)

        //获取sp
        boolean = getSharedPreferences("loging", AppCompatActivity.MODE_PRIVATE).getBoolean(
            "log",
            false
        )
        //加载按钮
        initButton()
    }


    private fun initButton() {
        main_home.addselectimage(R.mipmap.one2)
            .addtext("主页", -0xcccccd).show()
        main_class.addselectimage(R.mipmap.two2)
            .addtext("类型", -0xcccccd).show()
        main_car.addselectimage(R.mipmap.three2)
            .addtext("购物车", -0xcccccd).show()
        main_msg.addselectimage(R.mipmap.four2)
            .addtext("消息", -0xcccccd).show()
        main_main.addselectimage(R.mipmap.five2)
            .addtext("我的", -0xcccccd).show()
        main_home.setListenner(this)
        main_class.setListenner(this)
        main_car.setListenner(this)
        main_msg.setListenner(this)
        main_main.setListenner(this)
        showfragment(R.id.main_home)
    }

    override fun onItem(id: Int) {
        hindfragment()

        when (id) {
            R.id.main_home -> {
                showfragment(id)
            }
            R.id.main_class -> {
                showfragment(id)
            }
            R.id.main_car -> {

                if (boolean) {
                    chouse = 2
                    showfragment(id)

                } else {

                    var intent1 = Intent(this, LoginActivity::class.java)
                    intent1.putExtra("type", "car")
                    startActivity(intent1)
                }
            }
            R.id.main_msg -> {

                if (boolean) {
                    chouse = 3
                    showfragment(id)
                } else {
                    var intent1 = Intent(this, LoginActivity::class.java)
                    intent1.putExtra("type", "msg")
                    startActivity(intent1)
                }
            }
            R.id.main_main -> {

                if (boolean) {
                    chouse = 4
                    showfragment(id)
                } else {
                    var intent1 = Intent(this, LoginActivity::class.java)
                    intent1.putExtra("type", "main")
                    startActivity(intent1)
                }
            }
        }
    }

    private fun hindfragment() {
        var sf = supportFragmentManager.beginTransaction()
        if (homeFragment != null) {
            sf.hide(homeFragment)
        }
        if (classFragment != null) {
            sf.hide(classFragment)
        }
        if (carFragment != null) {
            sf.hide(carFragment)
        }
        if (msgFragment != null) {
            sf.hide(msgFragment)
        }
        if (mainFragment != null) {
            sf.hide(mainFragment)
        }
        sf.commit()
    }

    override fun onStart() {
        super.onStart()
        if (chouse == 0) {
            showfragment(R.id.main_home)
        }
        if (chouse == 1) {
            showfragment(R.id.main_class)
        }
        if (boolean) {
            if (chouse == 2) {
                loginshowfragment()
            }
            if (chouse == 3) {
                loginshowfragment()
            }
            if (chouse == 4) {
                loginshowfragment()
            }
        }


    }

    fun loginshowfragment() {
        when (type) {
            "car" -> {
                showfragment(R.id.main_car)
                LjlUtils.Ljl_Loge("car")
            }
            "home" -> {
                LjlUtils.Ljl_Loge("home")
            }
            "finishlogin" -> {
//                showfragment(R.id.main_main)
            }
            "msg" -> {
                LjlUtils.Ljl_Loge("msg")
                showfragment(R.id.main_msg)

            }
            "main" -> {
                LjlUtils.Ljl_Loge("main")
                showfragment(R.id.main_main)
            }


        }
    }

    fun showfragment(id: Int) {

        var sf = supportFragmentManager.beginTransaction()
        when (id) {
            R.id.main_home -> {
                chouse = 0

                news()
                if (homeFragment.isAdded) {
                    sf.show(homeFragment)
                } else {
                    sf.add(R.id.main_frag, homeFragment)
                    sf.show(homeFragment)
                }
            }
            R.id.main_class -> {
                chouse = 1

                peopre()
                if (classFragment.isAdded) {
                    sf.show(classFragment)
                } else {
                    sf.add(R.id.main_frag, classFragment)
                    sf.show(classFragment)
                }
            }
            R.id.main_car -> {

                video()
                if (carFragment.isAdded) {
                    sf.show(carFragment)
                } else {
                    sf.add(R.id.main_frag, carFragment)
                    sf.show(carFragment)
                }

            }
            R.id.main_msg -> {

                live()
                if (msgFragment.isAdded) {
                    sf.show(msgFragment)
                } else {
                    sf.add(R.id.main_frag, msgFragment)
                    sf.show(msgFragment)
                }

            }
            R.id.main_main -> {

                mine()
                if (mainFragment.isAdded) {
                    sf.show(mainFragment)
                } else {
                    sf.add(R.id.main_frag, mainFragment)
                    sf.show(mainFragment)
                }
            }
        }
        sf.commit()

    }

    //-----------------------------------------广播接收者-------------------------------------
    inner class MyBro : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            if (intent.action == "user_login") {
                var booleanExtra = intent.getBooleanExtra("login_dialog", false)
                if (booleanExtra) {
                    type = intent.getStringExtra("type")

                    when (type) {
                        "car" -> {
                            chouse = 2
                        }
                        "msg" -> {
                            chouse = 3
                        }
                        "main" -> {
                            chouse = 4
                        }
                        "home" -> {
                            chouse = 0
                        }
                        "finishlogin" -> {
                            chouse = 5
                        }


                    }

//                    LjlUtils.Ljl_Loge("登录了--" + type)
                    boolean =
                        getSharedPreferences("loging", AppCompatActivity.MODE_PRIVATE).getBoolean(
                            "log",
                            false
                        )

                } else {
                    type = intent.getStringExtra("type")
                    chouse = 0

                    boolean =
                        getSharedPreferences("loging", AppCompatActivity.MODE_PRIVATE).getBoolean(
                            "log",
                            false
                        )

//                    LjlUtils.Ljl_Loge("退出登录--" + type)
                }

            }
            if (intent.action == "login_finish") {

                type = intent.getStringExtra("type")
//                LjlUtils.Ljl_Loge("返回键--" + type)


            }
        }

    }


    fun news() {
        main_home.selectbar(R.mipmap.one, Color.RED)
        main_class.unselectbar(R.mipmap.two2, -0xcccccd)
        main_car.unselectbar(R.mipmap.three2, -0xcccccd)
        main_msg.unselectbar(R.mipmap.four2, -0xcccccd)
        main_main.unselectbar(R.mipmap.five2, -0xcccccd)
    }

    fun peopre() {
        main_home.unselectbar(R.mipmap.one2, -0xcccccd)
        main_class.selectbar(R.mipmap.two, Color.RED)
        main_car.unselectbar(R.mipmap.three2, -0xcccccd)
        main_msg.unselectbar(R.mipmap.four2, -0xcccccd)
        main_main.unselectbar(R.mipmap.five2, -0xcccccd)
        var sf = supportFragmentManager.beginTransaction()
    }

    fun video() {
        main_home.unselectbar(R.mipmap.one2, -0xcccccd)
        main_class.unselectbar(R.mipmap.two2, -0xcccccd)
        main_car.selectbar(R.mipmap.three, Color.RED)
        main_msg.unselectbar(R.mipmap.four2, -0xcccccd)
        main_main.unselectbar(R.mipmap.five2, -0xcccccd)
    }

    fun live() {
        main_home.unselectbar(R.mipmap.one2, -0xcccccd)
        main_class.unselectbar(R.mipmap.two2, -0xcccccd)
        main_car.unselectbar(R.mipmap.three2, -0xcccccd)
        main_msg.selectbar(R.mipmap.four, Color.RED)
        main_main.unselectbar(R.mipmap.five2, -0xcccccd)
    }

    fun mine() {
        main_home.unselectbar(R.mipmap.one2, -0xcccccd)
        main_class.unselectbar(R.mipmap.two2, -0xcccccd)
        main_car.unselectbar(R.mipmap.three2, -0xcccccd)
        main_msg.unselectbar(R.mipmap.four2, -0xcccccd)
        main_main.selectbar(R.mipmap.five, Color.RED)
    }
}